rem  "功能： 剪切掉固定长度的片头和片尾"
rem  "配置： 1.把ffmpeg放入PATH环境变量"
rem  "      2.把本文件放入媒体文件目录"
rem  "      3.输出文件存放在当前目录下的TMP_DIR目录下"

rem 设置延迟变量
setlocal enabledelayedexpansion  

rem  "s1:片头长度    s2:片尾长度"
set "s1=00:00:40.000"
set "s2=00:00:20.000"

rem 计算片尾的时间长度
for /f "tokens=1-4delims=:." %%a in ("%s2%") do (
    rem set /a "t2=(1%%a%%100*3600+1%%b%%100*60+1%%c%%100)*1000+1%%d%%1000"
   set /a "t2=(1%%a%%100*3600+1%%b%%100*60+1%%c%%100)*1000+1%%d%%1000"
)

echo %t2%

rem 创建目录
md TMP_DIR  2>nul

rem 按媒体文件类型，修改文件后缀
for %%f in (*.m4a) do (
    for /f "tokens=2-5delims=:., " %%a in ('ffmpeg -i %%f 2^>^&1 ^| findstr Duration') do (
        set /a "t=(1%%a%%100*3600+1%%b%%100*60+1%%c%%100)*1000+1%%d0%%1000,t-=t2,ms=t%%1000,t/=1000"
        set /a h=t/3600,m=t%%3600/60,s=t%%60,h+=100,m+=100,s+=100,ms+=1000
        set  "end_time=!h:~1!:!m:~1!:!s:~1!.!ms:~1!"
        echo !end_time!
        echo ----------%%f %%a:%%b:%%c.%%d  !end_time!-----------
        ffmpeg -i "%%f" -ss %s1% -to !end_time! -vcodec copy -acodec copy "TMP_DIR\%%f" -y      
    )
)
